// pages/index/article-details/article-details.js
var app =getApp()
let wxParse = require('../../utils/wxParse/wxParse.js');
const common = require('../../utils/login.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isIpx: app.globalData.isIphoneX ? true : false,
    headerImg: '../../image/articleDetail.png',
    articleImg: '../../image/articleDetail.png',
    voteImg: '../../image/detailsVoting.png',
    vote: false,
    variable: {
      userBtn: true
    },
    articleInfo: {},
    id: '' // 文章id
  },
  onLoad: function (options) {
    console.log(options);
    this.setData({
      id: options.id
    })
    this.isUserInfo();
    this.getArticleDetail(options.id);
  },
  // 获取文章详情
  getArticleDetail(id) {
    let that = this;
    wx.request({
      url: 'https://m.feawin.com/vote/content/' + id,
      data: '',
      method: 'get',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        // 'custom': JSON.stringify(header),
        'Authorization': 'Bearer ' + wx.getStorageSync('userInfo')
      },
      success: function (res) {
        console.log(res.data);
        let article = res.data.content;
        wxParse.wxParse('article', 'html', article, that, 0);
        that.setData({
          articleInfo: res.data
        })
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  // 判断是否有用户信息
  isUserInfo() {
    let userInfo = wx.getStorageSync('userInfo');
    let userBtn = 'variable.userBtn';
    if (userInfo) {
      this.setData({
        [userBtn]: false
      })
    }
  },
  // 登录
  bindGetUserInfo(e) {
    console.log(1);
    let id = this.data.id;
    console.log(id);
    let that = this;
    let userInfo = wx.getStorageSync('userInfo');
    if (!userInfo) {
      common.wxLogin().then(data => {
        console.log(data);
        let userBtn = 'variable.userBtn';
        that.setData({
          token: data.token,
          [userBtn]: false
        })
        wx.setStorageSync('userInfo', data.token); // 保存用户信息
        that.Voting(id);  // 登录之后调用投票
      });
    }
  },
  // 点击投票
  toupiao() {
    console.log(2);
    let id = this.data.id;
    this.Voting(id);
  },
  // 投票请求封装
  Voting: function (id) {
    console.log(id);
    let that = this;
    wx.request({
      url: 'https://m.feawin.com/vote/add/' + id,
      data: '',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        // 'custom': JSON.stringify(header),
        'Authorization': 'Bearer ' + wx.getStorageSync('userInfo')
      },
      success: function (res) {
        console.log(res)
        wx.showToast({
          title: '投票成功!',
          icon: 'success',
          duration: 2000
        })
        that.getArticleDetail(that.data.id);
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  // 已投票
  Voted() {
    console.log('已投票');
  },
  DetailVoting: function (e) {
    var vote = this.data.vote
    var voteImg = this.data.voteImg
    if (!vote) {
      vote = true
      voteImg = '../../image/detailsVoted.png'
      this.setData({
        vote: vote,
        voteImg: voteImg
      })
      wx.showToast({
        title: '投票成功!',
        icon: 'success',
        duration: 2000
      })
    }
  },
})